package com.xspace.android.imcommon.interfaces;

import com.xspace.android.imcommon.entity.PushMsg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class PushMsgMg implements IToPushListener {
	private ServiceManager mServiceManager = null;
	private Map<String, IToPushListener> mListenerMap = new HashMap<String, IToPushListener>();

	private PushMsgMg() {
		mServiceManager = ImServiceConnection.getInstance().getImServiceConnection();
	}

	private static PushMsgMg instance = null;

	public static PushMsgMg getInstance() {
		if (null == instance) {
			instance = new PushMsgMg();
		}
		return instance;
	}

	public void initPush(String ClassName, IToPushListener mListener) {
		if (mListener != null) {
			mListenerMap.put(ClassName, mListener);
			try {
				mServiceManager.initPush(PushMsgMg.this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
	public void removePush(String ClassName) {
			mListenerMap.remove(ClassName);
	}
	/**
	 * 推送类型列表
	 */
	public List<String> getMsgBodyTypeArr() {
		try {
			return mServiceManager.getMsgBodyTypeArr();
		} catch (Exception e) {
			return new ArrayList<String>();
		}

	}

	/**
	 * 获取信息列表
	 * 
	 */
	public ArrayList<PushMsg> queryPushMsgs(String mPushUserID, String mPlatformID, String barejid, String value1, String value2,String key) {
		ArrayList<PushMsg> list;
		try {
			PushListAidl mPushListAidl = mServiceManager.queryPushChildList(mPushUserID, mPlatformID, barejid, value1, value2,key);
			list = mPushListAidl.respList;
		} catch (Exception e) {
			list = new ArrayList<PushMsg>();
		}
		return list;
	}
	
	/**
	 * 获取信息列表
	 * 
	 */
	public ArrayList<PushMsg> queryPushMsgsAll(String owner,String channelName) {
		ArrayList<PushMsg> list;
		try {
			PushListAidl mPushListAidl = mServiceManager.queryPushChildAllList(owner,channelName);
			list = mPushListAidl.respList;
		} catch (Exception e) {
			list = new ArrayList<PushMsg>();
		}
		return list;
	}

	/**
	 * 修改推送信息
	 */
	public void saveOrUpdate(PushMsg info, String barejid) {
		try {
			mServiceManager.saveOrUpdatePush(barejid, new PushAidl(info));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 修改推送信息未读为已读
	 */
	public void updateUnReadPushCount(PushMsg info, String barejid) {
		try {
			//Log.i("pushdb", "updateunreadcount pushmsgmg  PushMsg:"+info+" barejid:"+barejid);
			mServiceManager.updateUnReadPushCount(barejid,  new PushAidl(info));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 我的所有推送信息
	 */
	public List<PushMsg> queryLatestPushMsgs(String barejid,String channelName) {
		ArrayList<PushMsg> list;
		try {
			PushListAidl pushListAidl = mServiceManager.queryLatestPushMsgs(barejid,channelName);
			list = pushListAidl.respList;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList<PushMsg>();
		}
		return list;
	}

	/**
	 * 删除对话
	 */
	public void deleteSinglePushMsg(int msgId) {
		try {
			mServiceManager.deleteSinglePushMsg(msgId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void deletePushMsg(String mPlatformId, String pushUserID, String barejid) {
		try {
			mServiceManager.deletePushMsg(mPlatformId, pushUserID, barejid);
		} catch (Exception e) {
		}
	}

	@Override
	public void onToPushList(ArrayList<PushMsg> pushList) {
		// TODO Auto-generated method stub
		Iterator<Entry<String, IToPushListener>> iter = mListenerMap.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, IToPushListener> entry = iter.next();
			Object key = entry.getKey();
			IToPushListener mListener = mListenerMap.get(key);
			mListener.onToPushList(pushList);
		}
	}

	@Override
	public void onToPushInfo(PushMsg info) {
		// TODO Auto-generated method stub
		Iterator<Entry<String, IToPushListener>> iter = mListenerMap.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, IToPushListener> entry = iter.next();
			Object key = entry.getKey();
			IToPushListener mListener = mListenerMap.get(key);
			mListener.onToPushInfo(info);
		}
	}

}